package com.chuangmei.com.myvoidplay;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import pl.droidsonroids.gif.GifImageView;

public class MainActivity extends AppCompatActivity {

    private FullScreenVideoView mVV1;
    private GifImageView mGIV;
    private String path;
    List<String> permissionList = new ArrayList<>();
    String[] mPermissionList = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private RelativeLayout.LayoutParams mVideoViewLayoutParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFullscreen();
        setContentView(R.layout.activity_main);
        path = Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/video/智石科技案例-万达云基地.mp4";
        mVV1 = this.findViewById(R.id.mVV1);
        mGIV = this.findViewById(R.id.mGIV);
        mVV1.setVideoURI(Uri.parse(path));

        MediaController mediaController = new MediaController(MainActivity.this);
        mVV1.setMediaController(mediaController);
        //mediaController.setVisibility(View.GONE);
        //开始播放视频
        mVV1.start();

        //监听视频播放完的代码
        mVV1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mPlayer) {
                // TODO Auto-generated method stub
                mPlayer.start();
                mPlayer.setLooping(true);
            }
        });

        //        Glide.with(MainActivity.this).load(R.drawable.gif_test)
        //                .diskCacheStrategy(DiskCacheStrategy.ALL).into(mIV);
        initPermission();
    }

    public void setFullscreen() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    //    @Override
    //    public void onConfigurationChanged(Configuration newConfig) {
    //        super.onConfigurationChanged(newConfig);
    //        int rot = getWindowManager().getDefaultDisplay().getRotation();
    //        if(rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270){
    //            mVideoViewLayoutParams = (RelativeLayout.LayoutParams)mVV1.getLayoutParams();
    //            RelativeLayout.LayoutParams layoutParams =
    //                    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout
    // .LayoutParams.MATCH_PARENT);
    //            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    //            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    //            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    //            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    //            mVV1.setLayoutParams(layoutParams);
    //            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams
    // .FLAG_FULLSCREEN);
    //
    //        }else if(rot == Surface.ROTATION_0){
    //                        RelativeLayout.LayoutParams lp = new  RelativeLayout.LayoutParams(320,240);
    //                        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
    //            mVV1.setLayoutParams(mVideoViewLayoutParams);
    //        }
    //    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(attrs);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags &= (WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attrs);
            getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }


    }

    /**
     * 6.0以后请求SD卡权限
     */
    private void initPermission() {

        for (int i = 0; i < mPermissionList.length; i++) {
            if (ContextCompat.checkSelfPermission(this, mPermissionList[i]) != PackageManager
                    .PERMISSION_GRANTED) {
                permissionList.add(mPermissionList[i]);
            }
        }
        if (permissionList.isEmpty()) {//未授予的权限为空，表示都授予了
            Toast.makeText(this, "已经授权", Toast.LENGTH_LONG).show();
        } else {//请求权限方法
            String[] permissions = permissionList.toArray(new String[permissionList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(this, permissions, 101);
        }
    }

}

/**
 * 本地播放
 */


